import { companyList, categoryList } from './api';

export const formItems = [
  // {
  //   label: '活动ID',
  //   name: 'username',
  //   type: 'input',
  //   itemProps: {
  //     placeholder: '请填写活动ID',
  //   },
  // },
  {
    label: '所属栏目页ID',
    name: 'category_id',
    type: 'input',
  },
  {
    label: '所属企业',
    name: 'company_id',
    type: 'select',
    required: true,
    asyncOptions: {
      api: companyList,
      handleData(data) {
        return data.list.map((item) => {
          item.label = item.name;
          item.value = item.id;
          return item;
        });
      },
    },
  },
  {
    label: '活动名称',
    name: 'title',
    type: 'input',
    required: true,
  },
  {
    label: '活动图片',
    value: [],
    name: 'img',
    slot: 'img',
  },
  {
    label: '规则',
    name: 'rule',
    type: 'textarea',
  },
  {
    label: '活动起止时间',
    name: 'at',
    value: [],
    valueFields: ['start_at', 'end_at'],
    valueType: 'number',
    type: 'range-picker',
    itemProps: {
      format: 'YYYY-MM-DD HH:mm',
    },
  },
  {
    label: '奖品数量',
    name: 'prize_num',
    type: 'input-number',
    itemProps: {
      min: 0,
    },
  },
  {
    label: '兑奖有效期',
    name: 'use_at',
    value: [],
    valueFields: ['use_start_at', 'use_end_at'],
    valueType: 'number',
    type: 'range-picker',
    itemProps: {
      format: 'YYYY-MM-DD HH:mm',
    },
  },
  {
    label: '宣传模块',
    name: 'module',
    value: {
      PIC: [],
      VIDEO: [],
    },
    slot: 'module',
  },
  {
    label: '报名任务',
    name: 'tasks',
    value: [],
    slot: 'tasks',
  },
];

export const formItems2Rules = {
  username: [
    {
      required: true,
      message: 'HD开头，HD+日期+五位自然数 如HD+210709+00001，即HD21070900001',
      pattern: /^HD\d{6,}/,
    },
  ],
  category_id: [
    {
      required: true,
      message: '请填写所属栏目页ID',
    },
  ],
  company_id: [
    {
      required: true,
      message: '请填写所属企业',
    },
  ],
  title: [
    {
      required: true,
      message: '请填写活动名称',
    },
  ],
  img: [
    {
      required: true,
      type: 'array',
      message: '请上传活动图片',
      trigger: 'blur',
      validator: async (ruleObj, value, a) => {
        if (!value || value.length == 0) {
          return Promise.reject('请上传活动图片');
        } else {
          return Promise.resolve();
        }
      },
    },
  ],
  rule: [
    {
      required: true,
      message: '请输入活动规则',
    },
    {
      max: 5000,
    },
  ],
  at: [
    {
      type: 'array',
      message: '请设置活动起止日期',
      required: true,
    },
  ],
  prize_num: [
    {
      type: 'number',
      message: '请设置奖品数量',
      required: true,
    },
  ],
  use_at: [
    {
      type: 'array',
      message: '请设置兑奖有效期',
      required: true,
    },
  ],
  module: [
    {
      type: 'array',
      required: true,
      trigger: 'blur',
      validator: async (ruleObj, value, a) => {
        if (value.PIC.length !== 0 || value.VIDEO.length !== 0) {
          return Promise.resolve();
        } else {
          return Promise.reject('请上传宣传模块图片或视频至少一个');
        }
      },
    },
  ],
  tasks: [
    {
      type: 'array',
      message: '请设置报名任务',
      required: true,
    },
  ],
};

export const taskCols = [
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    slots: { customRender: 'action' },
  },
  {
    title: '类型',
    dataIndex: 'type_name',
    key: 'type_name',
  },
  {
    title: '顺序',
    dataIndex: 'sort',
    key: 'sort',
  },
  {
    title: '标题',
    dataIndex: 'title',
    key: 'title',
  },
  {
    title: '最多次数',
    dataIndex: 'finish_time',
    key: 'finish_time',
  },
  {
    title: '完成1次进度',
    dataIndex: 'percent',
    key: 'percent',
  },
];

export const taskCommonFormItems = [
  {
    label: '任务名称',
    name: 'title',
    type: 'input',
  },
  {
    label: '显示顺序',
    name: 'sort',
    type: 'input-number',
    itemProps: {
      min: 1,
      max: 9,
    },
  },
  {
    label: '完成1次进度',
    name: 'percent',
    type: 'input-number',
    itemProps: {
      placeholder: '请填写相应比例',
      min: 1,
      max: 100,
    },
  },
];

export const taskShareFormItems = [
  ...taskCommonFormItems,
  {
    label: '可完成次数',
    name: 'finish_time',
    type: 'input-number',
    value: 1,
    itemProps: {
      min: 1,
    },
  },
];

export const taskPageFormItems = [
  ...taskCommonFormItems,
  {
    label: '浏览秒数',
    name: 'view_duration',
    type: 'input-number',
    itemProps: {
      min: 5,
      max: 30,
    },
  },
  {
    label: '栏目页ID',
    name: 'category_id',
    type: 'select',
    asyncOptions: {
      api: categoryList,
      handleData(data) {
        return data.list.map((item) => {
          item.label = item.title;
          item.value = item.id;
          return item;
        });
      },
    },
  },
];

export const taskVideoFormItems = [
  ...taskCommonFormItems,
  {
    label: '浏览秒数',
    name: 'view_duration',
    type: 'input-number',
    itemProps: {
      min: 5,
      max: 30,
    },
  },
  {
    label: '可完成次数',
    name: 'finish_time',
    type: 'input-number',
    value: 1,
    itemProps: {
      min: 1,
    },
  },
  {
    label: '浏览类型',
    name: 'lookObj',
    type: 'object',
    value: {
      type: 'ASSIGN',
      ids: [''],
    },
    slot: 'componentids',
  },
];

export const taskNewsFormItems = [
  ...taskCommonFormItems,
  {
    label: '浏览秒数',
    name: 'view_duration',
    type: 'input-number',
    itemProps: {
      min: 5,
      max: 30,
    },
  },
  {
    label: '可完成次数',
    name: 'finish_time',
    type: 'input-number',
    value: 1,
    itemProps: {
      min: 1,
    },
  },
  {
    label: '浏览类型',
    name: 'lookObj',
    type: 'object',
    value: {
      type: 'ASSIGN',
      ids: [''],
    },
    slot: 'newsids',
  },
];

export const taskCommonRules = {
  title: [
    {
      required: true,
      message: '请填写任务名称',
    },
    {
      max: 20,
    },
  ],
  sort: [
    {
      required: true,
      message: '请设置顺序，数字越小越优先展示',
    },
  ],
  percent: [
    {
      required: true,
      message: '请设置相应比例',
    },
  ],
};

export const taskShareRules = {
  ...taskCommonRules,
  finish_time: [
    {
      required: true,
      message: '请设置可完成次数',
    },
  ],
};

export const taskPageRules = {
  ...taskCommonRules,
  view_duration: [
    {
      required: true,
      message: '请设置浏览秒数',
    },
  ],
  category_id: [
    {
      required: true,
      message: '请填写栏目ID',
    },
  ],
};

export const taskVideoRules = {
  ...taskCommonRules,
  view_duration: [
    {
      required: true,
      message: '请设置浏览秒数',
    },
  ],
  finish_time: [
    {
      required: true,
      message: '请设置分享次数',
    },
  ],
  lookObj: [
    {
      required: true,
      validator: async (ruleObj, value, a) => {
        if (value.type == 'ASSIGN' && Array.from(value.ids).some((m) => !m)) {
          return Promise.reject('请将组件ID填写完整');
        } else {
          return Promise.resolve();
        }
      },
    },
  ],
};

export const taskNewsRules = {
  ...taskCommonRules,
  view_duration: [
    {
      required: true,
      message: '请设置浏览秒数',
    },
  ],
  finish_time: [
    {
      required: true,
      message: '请设置分享次数',
    },
  ],
  lookObj: [
    {
      required: true,
      validator: async (ruleObj, value, a) => {
        if (value.type == 'ASSIGN' && Array.from(value.ids).some((m) => !m)) {
          return Promise.reject('请将组件ID填写完整');
        } else {
          return Promise.resolve();
        }
      },
    },
  ],
};

export const taskTypes = [
  {
    label: '分享类',
    value: 'TASK_TYPE_SHARE',
  },
  {
    label: '页面类',
    value: 'TASK_TYPE_WEB',
  },
  {
    label: '视频类',
    value: 'TASK_TYPE_VIDEO',
  },
  {
    label: '新闻类',
    value: 'TASK_TYPE_NEWS',
  },
];

export const videoType = [
  {
    label: '指定视频',
    value: 'ASSIGN',
  },
  {
    label: '所有视频',
    value: 'ALL',
  },
];

export const newsTyoe = [
  {
    label: '指定新闻',
    value: 'ASSIGN',
  },
  {
    label: '所有新闻',
    value: 'ALL',
  },
];
